import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { decrypt } from '@/utils/rsaEncrypt'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  user: {},
  roles: [],
	// 第一次加载菜单时用到
	loadMenus: false
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_USER: (state, user) => {
	  state.user = user
	},
	SET_ROLES: (state, roles) => {
	  state.roles = roles
	},
	SET_LOAD_MENUS: (state, loadMenus) => {
	  state.loadMenus = loadMenus
	}
}

const actions = {
  //登录
  login({ commit }, userInfo) {
//  const { username, code, uuid } = userInfo
    const pwd = decrypt(userInfo.password)
    let user = Object.assign({}, userInfo, {username: userInfo.username.trim(), password: pwd})
    const rememberMe = userInfo.rememberMe
    delete user.rememberMe
    return new Promise((resolve, reject) => {
      login(user).then(response => {
      	const { data } = response
      	
      	setToken(data.token, rememberMe)
	      commit('SET_TOKEN', data.token)
	      setUserInfo(data.user, commit)
	      // 第一次加载菜单时用到， 具体见 src 目录下的 permission.js
	      commit('SET_LOAD_MENUS', true)
      	
//      const { data } = response
//      commit('SET_TOKEN', data.token)
//      setToken(data.token)
//      // 第一次加载菜单时用到， 具体见 src 目录下的 permission.js
//    	commit('SET_LOAD_MENUS', true)

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          reject('Verification failed, please Login again.')
        }
        const { username, avatar } = data
        commit('SET_NAME', username)
        commit('SET_AVATAR', avatar)
        setUserInfo(data, commit)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
  	debugger
  	return new Promise((resolve, reject) => {
	    commit('SET_TOKEN', '')
	    commit('SET_ROLES', [])
	    removeToken()
	    resetRouter()
	    resolve()
	  })
//  return new Promise((resolve, reject) => {
//    logout(state.token).then(() => {
//      commit('SET_TOKEN', '')
//      removeToken()
//      resetRouter()
//      resolve()
//    }).catch(error => {
//      reject(error)
//    })
//  })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  },

	updateLoadMenus({ commit }) {
	  return new Promise((resolve, reject) => {
	    commit('SET_LOAD_MENUS', false)
	  })
	}
}

export const setUserInfo = (res, commit) => {
  // 如果没有任何权限，则赋予一个默认的权限，避免请求死循环
  if (res.roles.length === 0) {
    commit('SET_ROLES', ['ROLE_SYSTEM_DEFAULT'])
  } else {
    commit('SET_ROLES', res.roles)
  }
  commit('SET_USER', res)
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

